package study.springcloud.gateway.support.utils;

import org.slf4j.MDC;

import java.util.Map;

public class MdcUtils {

    private static final String REQUEST_ID = "request_id";

    public static void put(String requestId) {
        MDC.put(REQUEST_ID, requestId);
    }

    public static void remove() {
        MDC.remove(REQUEST_ID);
    }

    /**
     * 包装
     */
    public static Runnable wrap(Runnable runnable) {
        Map<String, String> mdc = MDC.getCopyOfContextMap();
        return () -> {
            MDC.setContextMap(mdc);
            runnable.run();
        };
    }
}
